#include "um_basic_types.h"
#include "umapi_os.h"
#include "umapi_libc.h"
#include "umapi_file.h"
#include "umlog.h"
#include <stdarg.h> 
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>

#define ASYNCFLUSH 0
static UM_HANDLE fout = 0, fProcout = 0;

#define STRLEVEL(l) l==4?"debug":l==3?"info":l==2?"warn":l==1?"err":"  "

static inline  UM_VOID writelog(UM_HANDLE fd, UM_CHAR *buff)
{
	if(fd == 0)
		return ;
	UM_S32 s32ret = umapi_file_write(fd, buff, umapi_strlen(buff));
}
#if !ASYNCFLUSH
static UM_VOID flushlog(UM_HANDLE fd)
{
	//umapi_file_flush(fd);
}
#else
static UM_VOID flushlogfile(UM_HANDLE fd)
{
	umapi_file_flush(fd);
}
#endif
static UM_HANDLE timerhandle;

UM_VOID debugproc(int level, UM_CHAR *target, int modelevel, int printtime, UM_CHAR * fmt, ...)
{
	if(fProcout == 0)
	{
		umapi_file_fopen("proc.log","ab+",&fProcout);
		if(fProcout)
		{
			writelog(fProcout,"======start proc=====\r\n");
		}
	}
	UM_CHAR logbuff[4 * 1024];
	UM_S32 ret = 0;	
	va_list args;
	va_start(args,fmt);
	ret = vsnprintf(logbuff, 4 * 1024, fmt, args);
	va_end(args);
	umapi_file_fwrite(logbuff,1,umapi_strlen(logbuff), fProcout);
	umapi_file_flush(fProcout);
	return ;
}
static UM_CHAR* toStr(UM_S32 pid)
{
	UM_CHAR buff[10];
	umapi_sprintf(buff, "%d", pid);
	return buff;
}
#include<stdio.h>
#include<time.h>
#include<sys/time.h>

static UM_VOID printDayTime()
{
	struct timeval us;
	struct tm t;
	gettimeofday(&us,NULL);
	char date_time[70]; /*%Y-%m-%d */
	UM_CHAR timeDebugBuff[80];
	strftime(date_time, sizeof(date_time), "%Y-%m-%d %H:%M:%S", localtime_r(&us.tv_sec, &t));
	umapi_sprintf(timeDebugBuff, "%s %d", date_time, us.tv_usec);
	writelog(fout , timeDebugBuff);
}
static inline UM_VOID printTime()
{
	struct timeval us;
	struct tm t;
	gettimeofday(&us,NULL);
	char date_time[70]; /*%Y-%m-%d */
	UM_CHAR timeDebugBuff[80];
	strftime(date_time, sizeof(date_time), "%H:%M:%S", localtime_r(&us.tv_sec, &t));
	umapi_sprintf(timeDebugBuff, "%s %d", date_time, us.tv_usec);
	writelog(fout , timeDebugBuff);

}
static UM_HANDLE logMutex = UM_NULL;
UM_VOID debug(int level, UM_CHAR *target, int modelevel, int printtime, UM_CHAR * fmt, ...)
{
	if(fout == 0)
	{
		umapi_os_mutex_create(UM_NULL,1, &logMutex);
		umapi_file_open("mediaServer.log",UM_FILE_APPEND | UM_FILE_EXEC | UM_FILE_WRONLY,"777",&fout);
		if(fout)
		{
			printDayTime();
			writelog(fout,"======start Application=====\r\n");
#if !ASYNCFLUSH			
			flushlog(fout);
#endif
#if ASYNCFLUSH			
			umapi_os_timer_create(UM_NULL,UM_FALSE, &timerhandle);
			umapi_os_timer_settime(timerhandle, 100);
			umapi_os_timer_setcallback(timerhandle, flushlogfile,fout);
			umapi_os_timer_activate(timerhandle,UM_TRUE);
#endif			
		}
	}
	if(level!= 100 && (level > modelevel || modelevel == 0))
		return ;
	//umapi_os_mutex_lock(logMutex, -1);
	UM_CHAR logbuff[4 * 1024];
	UM_S32 ret = 0;	
	va_list args;
	va_start(args,fmt);
	ret = vsnprintf(logbuff, 4 * 1024, fmt, args);
	va_end(args);
	if(ret <0 || ret>4 * 1024)
	{
		return ;
	}
	if(printtime)
	{
		printTime();
	}
	if(level!= 100)
	{
/*		writelog(fout , "[");
		writelog(fout, toStr(getpid()));
		writelog(fout , "]");
*/		
		UM_CHAR buff[200];
		umapi_sprintf(buff, "[%s][%s]",target,STRLEVEL(level));
		writelog(fout, buff);
/*		writelog(fout , "[");
		writelog(fout, target);
		writelog(fout , "]");
		writelog(fout , "[");
		writelog(fout, STRLEVEL(level));
		writelog(fout , "]");
*/		
	}
	writelog(fout, logbuff);
	if(level!= 100)
	{
		writelog(fout, "\r\n");
	}
#if ASYNCFLUSH
#else
	flushlog(fout);
#endif
//	umapi_os_mutex_unlock(logMutex);
	return ;
}

